﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace GIMachineGenerator
{
    public class GIMachine
    {
        private string _name = "";
        private List<GIMachineWordState> _states = new List<GIMachineWordState>();


        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        public List<GIMachineWordState> States
        {
            get
            {
                return _states;
            }
        }

        public GIMachine Clone(string sNewMachineName)
        {
            GIMachine m = new GIMachine();
            m.Name = sNewMachineName;

            foreach (GIMachineWordState S in this.States)
            {
                m.States.Add(S.Clone());
            }

            return m;
        }


        public GIMachineWordState[] Suggestions(string sPartialInput)
        {
            throw new System.NotImplementedException();
        }

        public GIMatchCollection ProcessText(string sInput)
        {
            throw new System.NotImplementedException();
        }

        public static void WriteNodeVale(string nodeName, string value, ref System.Xml.XmlWriter oWriter)
        {
            oWriter.WriteStartElement(nodeName);
            oWriter.WriteString(value);
            oWriter.WriteEndElement();
        }

        public string ToXML()
        {
            StringBuilder sbResult = new StringBuilder();
            System.Xml.XmlWriterSettings oSettings = new System.Xml.XmlWriterSettings();
            oSettings.Encoding = Encoding.ASCII;
            oSettings.Indent = true;
            System.Xml.XmlWriter oWriter = System.Xml.XmlWriter.Create( sbResult, oSettings);

            oWriter.WriteStartElement("GIMachine");
            GIMachine.WriteNodeVale("Name", this.Name, ref oWriter);
            foreach (GIMachineWordState S in this.States)
            {
                S.ToXmlWriter(ref oWriter);
            }

            oWriter.WriteEndElement();

            oWriter.Close();

            return sbResult.ToString();
        }

        
    }
}
